<?php

use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;

class CreateCommentsTable extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->bigIncrements('id')->unsigned();
            $table->unsignedBigInteger('user_id')->comment('用户ID')->index();
            $table->unsignedBigInteger('article_id')->comment('文章ID')->index();
            $table->unsignedBigInteger('parent_id')->comment('父评论ID')->nullable()->index();
            $table->text('content')->comment('评论内容');
            $table->unsignedInteger('like_count')->default(0)->comment('点赞数');
            $table->enum('status', ['pending', 'approved', 'rejected'])->default('pending')->comment('审核状态')->index();
            $table->timestamps();

            $table->index('created_at');

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');
            $table->foreign('parent_id')->references('id')->on('comments')->onDelete('cascade');

            $table->comment('评论表');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('comments');
    }
}
